package ebj.common.annotation.demo_data_provider_manager.data_provider;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;

import org.apache.catalina.Host;
import org.apache.kylin.jdbc.Driver;
import org.junit.Test;

import com.facebook.presto.jdbc.PrestoConnection;
import com.facebook.presto.jdbc.PrestoDriver;
import com.facebook.presto.jdbc.PrestoStatement;

import ebj.common.annotation.demo_data_provider_manager.anno.ProviderName;

@ProviderName("Kylin")
public class PrestoDataProvider extends DataProvider {
    String host = "10.2.26.61:18080";
    String user = "hive";
    String pass = null;

    @Override
    public String[][] getData(Map<String, String> datasourceConfig,
            Map<String, String> query) throws Exception {
        return null;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getClass().getSimpleName() + "[" + this.host + ", "
                + user + ", "
                + pass + "]";
    }
    
    @Test
    public void testPrestoJDBC() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
//        jdbc:presto://master.host:port/catalog/schema
        String presto_proj_url = "jdbc:presto://10.2.26.61:18080/system/runtime";
//        String presto_proj_url = "jdbc:presto://10.8.104.99:18080/system/runtime";
        
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        String JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
        Class.forName(JDBC_DRIVER);
        PrestoConnection conn = (PrestoConnection) DriverManager.getConnection(presto_proj_url, user, null);
        PrestoStatement state = (PrestoStatement) conn.createStatement();
        
        ResultSet resultSet = state.executeQuery("select * from nodes");
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            System.out.println(metaData.getColumnName(i + 1));
        }
    }
    
    @Test
    public void testPrestoJDBC2() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
//        jdbc:presto://master.host:port/catalog/schema
        String presto_proj_url = "jdbc:presto://10.2.26.61:18080";
        
//        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
//        String JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
//        Class.forName(JDBC_DRIVER);
        
        Connection connection = DriverManager.getConnection(presto_proj_url,"root",null);  
        connection.setCatalog("hive");  
        Statement stmt = connection.createStatement();  
       ResultSet rs = stmt.executeQuery("show tables");  
        while (rs.next()) {  
            System.out.println(rs.getString(1));  
        }  
        rs.close();  
        connection.close();  
    }
}
